<!-- 结算 – 发票录入 - 弹窗组件 -->
<template>
  <div>
    <CustomDialog v-model:visible="isShowDialog">
      <div class="dinerDialog-content">
        <div class="title">发票录入</div>
        <div class="form-box">
          <div class="form-item">
            <div class="desc">发票单号</div>
            <div class="dialog-input">
              <input
                class="input"
                v-model.trim="invoiceForm.invoice_id"
                type="text"
                placeholder="请输入发票单号"
                @focus="onInputFocus($event, 'invoice_id', invoiceForm.invoice_id)"
              />
              <img
                class="image"
                src="@/assets/images/home/input-clear.png"
                alt=""
                @click="handleClearInput(1)"
                v-if="invoiceForm.invoice_id"
              />
            </div>
          </div>
          <div class="form-item">
            <div class="desc">发票金额</div>
            <div class="dialog-input">
              <span>￥</span>
              <input
                class="input"
                v-model.trim="invoiceForm.price"
                type="text"
                placeholder="请输入发票金额"
                @focus="onInputFocus($event, 'price', invoiceForm.price)"
              />
              <img
                class="image"
                src="@/assets/images/home/input-clear.png"
                alt=""
                @click="handleClearInput(2)"
                v-if="invoiceForm.price"
              />
            </div>
          </div>
        </div>
      </div>
      <!-- 数字键盘组件 -->
      <NumberKeyboard
        v-model:keyword="currentInputVal"
        @onInputKeyboard="onInputKeyboard"
        @onCancel="handleClearInput"
        @onConfirm="onConfirm"
        :loading="confirmLoading"
      />
    </CustomDialog>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, watch } from 'vue'
import { ElMessage } from 'element-plus'
import CustomDialog from '@/components/CustomDialog/Index.vue'
import NumberKeyboard from '@/components/NumberKeyboard/Index.vue'
import { formTestFun, throttle } from '@/utils/toolsValidate'
import { invoiceAdd } from '@/api/desktop'

interface Props {
  showDialog: boolean
  orderId: string
  orderPaymentStatusInfo?: any
}

interface Emits {
  (e: 'update:showDialog', value: boolean): void
  (e: 'onConfirm'): void
}

const props = withDefaults(defineProps<Props>(), {
  showDialog: false,
  orderId: '',
  orderPaymentStatusInfo: null
})

const emit = defineEmits<Emits>()

const isShowDialog = computed({
  get: () => props.showDialog,
  set: val => emit('update:showDialog', val)
})

// 发票表单数据
const invoiceForm = ref({
  order_id: '', // 订单id
  invoice_id: '', // 发票单号
  price: '', // 发票金额
  type: 0 // 发票类型(0:普通发票 1:定金发票)
})

// 判断目前聚焦在哪个input输入框
const inputFocusType = ref<string | null>(null)
// 获取当前聚焦的input的值
const currentInputVal = ref('')
// 确认按钮loading状态
const confirmLoading = ref(false)

// 监听订单支付信息，自动填充发票金额
watch(
  () => props.orderPaymentStatusInfo,
  val => {
    if (val && val.orderInfo && val.orderInfo.pay_money) {
      invoiceForm.value.price = val.orderInfo.pay_money
    }
  },
  { immediate: true, deep: true }
)

// 判断input的聚焦事件在哪个input框上面
const onInputFocus = (event: Event, type: string, val: string) => {
  inputFocusType.value = type
  currentInputVal.value = val
}

// 接收数字键盘传过来的关键词
const onInputKeyboard = (val: string) => {
  const type = inputFocusType.value
  if (type === 'invoice_id') {
    invoiceForm.value.invoice_id = val
    return
  }
  if (type === 'price') {
    invoiceForm.value.price = val
  }
}

// 清空输入框
const handleClearInput = (type?: number) => {
  if (type === 1) {
    invoiceForm.value.invoice_id = ''
    return
  }
  if (type === 2) {
    invoiceForm.value.price = ''
    return
  }
  // 清空全部输入框
  invoiceForm.value.invoice_id = ''
  invoiceForm.value.price = ''
  currentInputVal.value = ''
}

// 确认提交事件
const onConfirm = throttle(async () => {
  // 表单验证
  const testForm = {
    发票单号: invoiceForm.value.invoice_id,
    发票金额: invoiceForm.value.price
  }
  if (formTestFun(testForm)) return

  confirmLoading.value = true
  try {
    invoiceForm.value.order_id = props.orderId
    const res = await invoiceAdd(invoiceForm.value)
    if (res.status === 200) {
      ElMessage.success('成功添加发票')
      emit('onConfirm')
      emit('update:showDialog', false)
    }
  } catch (error) {
    console.error('添加发票失败:', error)
  } finally {
    confirmLoading.value = false
  }
}, 3000)
</script>

<style lang="scss" scoped>
.dinerDialog-content {
  .title {
    font-size: 20px;
    font-family:
      PingFang SC,
      PingFang SC-Bold;
    font-weight: 700;
    text-align: center;
    color: #333333;
  }
  .form-box {
    padding: 0 6px;
    margin-bottom: 60px;
    margin-top: 10px;
    .form-item {
      display: flex;
      align-items: center;
      justify-content: space-between;
      margin-top: 30px;
      .desc {
        font-size: 16px;
        font-family:
          PingFang SC,
          PingFang SC-Regular;
        font-weight: 400;
        color: #333333;
      }
      .dialog-input {
        width: 420px;
        height: 48px;
        background: #ffffff;
        border: 1px solid #d2d2d2;
        border-radius: 12px;
        box-sizing: border-box;
        padding: 0 20px;
        display: flex;
        align-items: center;
        justify-content: space-between;

        .input {
          width: 400px;
          font-size: 14px;
          font-family:
            PingFang SC,
            PingFang SC-Regular;
          font-weight: 400;
          text-align: left;
          color: #333333;
          outline: none;
          border: none;
        }
        .image {
          flex-shrink: 0;
          width: 24px;
          height: 24px;
          cursor: pointer;
        }
      }
    }
  }
}
</style>
